#include<vector>
using namespace std;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<TreeNode*> stack1;
        vector<TreeNode*>stack2;
        vector<vector<int>>res;
        if (root == NULL) return res;
        stack1.push_back(root);
        bool pos = true;
        while (!stack1.empty()) {
            vector<int>tmp;
            while (!stack1.empty()) {
                auto node = stack1.back();
                stack1.pop_back();
                tmp.push_back(node->val);
                if (pos) {
                    if (node->left != NULL) stack2.push_back(node->left);
                    if (node->right != NULL) stack2.push_back(node->right);
                }
                else {
                    if (node->right != NULL) stack2.push_back(node->right);
                    if (node->left != NULL) stack2.push_back(node->left);
                }
            }
            pos = !pos;
            res.push_back(tmp);
            swap(stack1, stack2);
        }
        return res;
    }
};